package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.entity.ServiceGroup;
import com.erpanderp.wam.util.HibernateUtil;

public class ServiceDAO {
	/*
	 * 
	 * 
	 */
	
	public Service createService(Service service){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		session.setFlushMode(FlushMode.AUTO);
		session.save(service);
		session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return service;
		
		
	}
	/*
	 * 
	 * 
	 */

	public boolean isServiceNameAvailable(String serviceName){
		boolean isServiceNameAvailable= false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.getTransaction().begin();
			
			String queryString = "from Service s where s.serviceName = :serviceName";
			
			Query query = session.createQuery(queryString).setString("serviceName", serviceName);
			
			if(query.uniqueResult() == null){
				isServiceNameAvailable = true;
			}else{
				isServiceNameAvailable = false;
			}						
			session.close();
			return isServiceNameAvailable;
			
		}catch(Exception e){
			e.printStackTrace();
			return isServiceNameAvailable;
		}
		
	}
	/*
	 *  
	 */
	public boolean createServices(List<Service> servicesList){
		boolean servicesSaved = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			for(Service service: servicesList){
				session.save(service);
				
			}
			session.getTransaction().commit();
			servicesSaved = true;
			
		}catch(Exception e){
			e.printStackTrace();
			return servicesSaved;
		}
		return servicesSaved;
	}
	/*
	 *  
	 */
	public boolean updateService(Service service){
		boolean serviceUpdated;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.update(service);
			session.getTransaction().commit();
			serviceUpdated = true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return serviceUpdated;
		
	}
	
	public List<Service> getAllServices()
	{
		List<Service> services = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
			String queryString = "from Service s order by s.serviceName";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			services = (List<Service>) query.list();	
			
			session.close();
			return services;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	public List<Service> getAllServicesByServiceGroupID(Long serviceGroupId) {
		List<Service> services = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Service s where s.serviceGroup.serviceGroupId=:serviceGroupId order by s.serviceName";
			Query query = session.createQuery(queryString).setLong("serviceGroupId",
					serviceGroupId);
			query.setCacheable(false);
			services = (List<Service>) query.list();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return services;
	}
	
	public Service getServiceById(Long serviceId)
	{
		Service service = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			service = (Service) session.load(Service.class, serviceId);
			session.close();
			return service;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public List<Service> getServicesByModuleId(Long moduleId)
	{
		List<Service> services = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			
			String queryString = "from Service s where s.module.moduleId = :moduleId";
			Query query = session.createQuery(queryString).setParameter("moduleId", moduleId);
			query.setCacheable(false);
			services = (List<Service>) query.list();
			return services;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public static void main(String ar[]){
		for(Service service : new ServiceDAO().getAllServices()){
			System.out.println(service.getServiceName());
		}
			
	}

}
